package com.tom.chain_of_responsibility.v3.factory;

import com.tom.chain_of_responsibility.v3.GeneralChainHandler;
import org.springframework.util.Assert;

import java.util.List;

/**
 * @author zx
 * @date 2024-10-10 21:45
 */
public class GeneralFactory<T, R> {
    GeneralChainHandler<T, R> first;

    public GeneralFactory(List<GeneralChainHandler<T, R>> requestHandlers) {
        Assert.notEmpty(requestHandlers, "requestHandlers can not be empty");
        for (int i = 0; i < requestHandlers.size() - 1; i++) {
            requestHandlers.get(i).setNext(requestHandlers.get(i + 1));
        }
        first = requestHandlers.get(0);
    }

    public R executeHandler(T t) {
        return first.handleRequest(t);
    }


}
